package io.rong.imkit.dialog

import android.content.Context
import android.graphics.Paint
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.custom.base.entity.OkHttpBodyEntity
import com.custom.base.http.OkHttpFromBoy
import com.custom.base.http.OkHttpManager
import com.custom.base.http.SDOkHttpResoutCallBack
import com.custom.base.util.IntentUtil
import io.rong.imkit.API
import io.rong.imkit.R
import io.rong.imkit.entity.AgreementEntity
import io.rong.imkit.entity.BuyMemberPageEntity
import io.rong.imkit.webview.WebViewActivity
import razerdp.basepopup.BasePopupWindow

class MemberUnitaryBuyDialog(var ctx: Context,var productCategory:Int, var listener :MemberUnitaryBuyListener) :  BasePopupWindow(ctx) {
    init {
        setContentView(R.layout.dialog_member_buy_unitary)
        initView()
    }

    private fun initView() {
        val dialogPurchaseNow = findViewById<TextView>(R.id.dialog_purchase_now)
        val dialogCancel = findViewById<ImageView>(R.id.dialog_close)
        val imgTop = findViewById<ImageView>(R.id.img_top)
        val itemTitle = findViewById<TextView>(R.id.item_title)
        val itemContent = findViewById<TextView>(R.id.item_content)

        val quarterContainer = findViewById<ConstraintLayout>(R.id.quarter_container)
        val itemQuarterTitle = findViewById<TextView>(R.id.item_quarter_title)
        val itemQuarterNumber = findViewById<TextView>(R.id.item_quarter_number)
        val itemQuarterName = findViewById<TextView>(R.id.item_quarter_name)
        val itemQuarterDiscountedPrice = findViewById<TextView>(R.id.item_quarter_discounted_price)
        val itemQuarterOriginalPrice = findViewById<TextView>(R.id.item_quarter_original_price)
        val itemQuarterDiscount = findViewById<TextView>(R.id.item_quarter_discount)

        val monthlyContainer = findViewById<ConstraintLayout>(R.id.monthly_container)
        val itemMonthlyTitle = findViewById<TextView>(R.id.item_monthly_title)
        val itemMonthlyNumber = findViewById<TextView>(R.id.item_monthly_number)
        val itemMonthlyName = findViewById<TextView>(R.id.item_monthly_name)
        val itemMonthlyDiscountedPrice = findViewById<TextView>(R.id.item_monthly_discounted_price)
        val itemMonthlyOriginalPrice = findViewById<TextView>(R.id.item_monthly_original_price)
        val itemMonthlyDiscount = findViewById<TextView>(R.id.item_monthly_discount)

        val weeksContainer = findViewById<ConstraintLayout>(R.id.weeks_container)
        val itemWeeksNumber = findViewById<TextView>(R.id.item_weeks_number)
        val itemWeeksName = findViewById<TextView>(R.id.item_weeks_name)
        val itemWeeksDiscountedPrice = findViewById<TextView>(R.id.item_weeks_discounted_price)

        val txtBuySingleTips = findViewById<TextView>(R.id.txt_buy_single_tips)
        var model: BuyMemberPageEntity.ProductExt? = null


        OkHttpManager.instance.requestInterface(object :OkHttpFromBoy{
            override fun addBody(requestBody: OkHttpBodyEntity) {
                requestBody.setPost(API.user_member_product_category_url)
                requestBody.add("productCategory",productCategory)
            }

        },object : SDOkHttpResoutCallBack<BuyMemberPageEntity>() {
            override fun onSuccess(entity: BuyMemberPageEntity) {
                itemTitle.text=entity.data.subscriptions[0].tip
                itemContent.text=entity.data.subscriptions[0].content

                when(productCategory){
                    2->imgTop.setImageResource(R.drawable.icon_dialog_flash_chat_pink)
                    3->imgTop.setImageResource(R.drawable.icon_dialog_private_photos_pink)
                    4->imgTop.setImageResource(R.drawable.icon_dialog_private_videos_pink)
                }
                for(i in 0 until  entity.data.productDescriptions.size){
                    if (entity.data.productDescriptions[i].check == true){
                        model = entity.data.productDescriptions[i]
                    }
                }

                quarterContainer.alpha=if (entity.data.productDescriptions[2].check == true) 1.0f else 0.6f
                quarterContainer.setBackgroundResource(if (entity.data.productDescriptions[2].check == true) R.drawable.shape_stroke_black_solid_pink_radius_12 else R.drawable.shape_stroke_pink_radius_12)
                itemQuarterTitle.setBackgroundResource(if (entity.data.productDescriptions[2].check == true) R.drawable.shape_half_top_solid_black_radius_6 else R.drawable.shape_half_top_solid_pink_radius_6)
                itemQuarterTitle.text=entity.data.productDescriptions[2].tip
                itemQuarterNumber.text= entity.data.productDescriptions[2].benefitNum
                itemQuarterName.text=entity.data.productDescriptions[2].benefitUnit
                itemQuarterDiscountedPrice.text= entity.data.productDescriptions[2].price
                itemQuarterOriginalPrice.text="${entity.data.productDescriptions[2].priceOriginal}"
                itemQuarterOriginalPrice.paint.flags = Paint.STRIKE_THRU_TEXT_FLAG
                itemQuarterDiscount.text=entity.data.productDescriptions[2].saving

                monthlyContainer.alpha=if (entity.data.productDescriptions[1].check == true) 1.0f else 0.6f
                monthlyContainer.setBackgroundResource(if (entity.data.productDescriptions[1].check == true) R.drawable.shape_stroke_black_solid_pink_radius_12 else R.drawable.shape_stroke_pink_radius_12)
                itemMonthlyTitle.setBackgroundResource(if (entity.data.productDescriptions[1].check == true) R.drawable.shape_half_top_solid_black_radius_6 else R.drawable.shape_half_top_solid_pink_radius_6)
                itemMonthlyTitle.text=entity.data.productDescriptions[1].tip
                itemMonthlyNumber.text= entity.data.productDescriptions[1].benefitNum
                itemMonthlyName.text=entity.data.productDescriptions[1].benefitUnit
                itemMonthlyDiscountedPrice.text= entity.data.productDescriptions[1].price
                itemMonthlyOriginalPrice.text="${entity.data.productDescriptions[1].priceOriginal}"
                itemMonthlyOriginalPrice.paint.flags = Paint.STRIKE_THRU_TEXT_FLAG
                itemMonthlyDiscount.text=entity.data.productDescriptions[1].saving

                weeksContainer.alpha=if (entity.data.productDescriptions[0].check == true) 1.0f else 0.6f
                weeksContainer.setBackgroundResource(if (entity.data.productDescriptions[0].check == true) R.drawable.shape_stroke_black_solid_pink_radius_12 else R.drawable.shape_stroke_pink_radius_12)
                itemWeeksNumber.text= entity.data.productDescriptions[0].benefitNum
                itemWeeksName.text=entity.data.productDescriptions[0].benefitUnit
                itemWeeksDiscountedPrice.text= entity.data.productDescriptions[0].price



                quarterContainer.setOnClickListener {
                    quarterContainer.alpha=1.0f
                    quarterContainer.setBackgroundResource(R.drawable.shape_stroke_black_solid_pink_radius_12)
                    itemQuarterTitle.setBackgroundResource(R.drawable.shape_half_top_solid_black_radius_6)
                    entity.data.productDescriptions[2].check=true
                    model = entity.data.productDescriptions[2]

                    monthlyContainer.alpha=0.6f
                    monthlyContainer.setBackgroundResource(R.drawable.shape_stroke_pink_radius_12)
                    itemMonthlyTitle.setBackgroundResource(R.drawable.shape_half_top_solid_pink_radius_6)
                    entity.data.productDescriptions[1].check=false

                    weeksContainer.alpha=0.6f
                    weeksContainer.setBackgroundResource(R.drawable.shape_stroke_pink_radius_12)
                    entity.data.productDescriptions[0].check=false

                }

                monthlyContainer.setOnClickListener {
                    quarterContainer.alpha=0.6f
                    quarterContainer.setBackgroundResource(R.drawable.shape_stroke_pink_radius_12)
                    itemQuarterTitle.setBackgroundResource(R.drawable.shape_half_top_solid_pink_radius_6)
                    entity.data.productDescriptions[2].check=false

                    monthlyContainer.alpha=1.0f
                    monthlyContainer.setBackgroundResource(R.drawable.shape_stroke_black_solid_pink_radius_12)
                    itemMonthlyTitle.setBackgroundResource(R.drawable.shape_half_top_solid_black_radius_6)
                    entity.data.productDescriptions[1].check=true
                    model = entity.data.productDescriptions[1]

                    weeksContainer.alpha=0.6f
                    weeksContainer.setBackgroundResource(R.drawable.shape_stroke_pink_radius_12)
                    entity.data.productDescriptions[0].check=false

                }

                weeksContainer.setOnClickListener {
                    quarterContainer.alpha=0.6f
                    quarterContainer.setBackgroundResource(R.drawable.shape_stroke_pink_radius_12)
                    itemQuarterTitle.setBackgroundResource(R.drawable.shape_half_top_solid_pink_radius_6)
                    entity.data.productDescriptions[2].check=false

                    monthlyContainer.alpha=0.6f
                    monthlyContainer.setBackgroundResource(R.drawable.shape_stroke_pink_radius_12)
                    itemMonthlyTitle.setBackgroundResource(R.drawable.shape_half_top_solid_pink_radius_6)
                    entity.data.productDescriptions[1].check=false

                    weeksContainer.alpha=1f
                    weeksContainer.setBackgroundResource(R.drawable.shape_stroke_black_solid_pink_radius_12)
                    entity.data.productDescriptions[0].check=true
                    model = entity.data.productDescriptions[0]

                }

                showPopupWindow()
            }
        })

        OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
            override fun addBody(requestBody: OkHttpBodyEntity) {
                requestBody.setPost(API.user_config_url)
                requestBody.add("code", 2)
            }
        }, object : SDOkHttpResoutCallBack<AgreementEntity>() {
            override fun onSuccess(entity: AgreementEntity) {
                val style = SpannableStringBuilder()
                style.append(ctx.getString(R.string.buy_member_unitary_tips))

                style.setSpan(object : ClickableSpan() {
                    override fun onClick(p0: View) {
                        val b = Bundle()
                        b.putString("url", entity.data.privacyPolicy)
                        b.putString(
                            "title",
                            ctx.resources.getString(R.string.privacy_policy)
                        )
                        b.putBoolean("isFull", true)
                        IntentUtil.startActivity(WebViewActivity::class.java, b)
                    }

                    override fun updateDrawState(ds: TextPaint) {
                        ds.isUnderlineText = true
                        ds.clearShadowLayer()
                    }
                }, 32, 46, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
                style.setSpan(object : ClickableSpan() {
                    override fun onClick(p0: View) {
                        val b = Bundle()
                        b.putString("url", entity.data.terms)
                        b.putString(
                            "title",
                            ctx.resources.getString(R.string.terms)
                        )
                        b.putBoolean("isFull", true)
                        IntentUtil.startActivity(WebViewActivity::class.java, b)
                    }

                    override fun updateDrawState(ds: TextPaint) {
                        ds.isUnderlineText = true
                        ds.clearShadowLayer()
                    }
                }, 51, 56, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

                txtBuySingleTips.text = style
                txtBuySingleTips.movementMethod = LinkMovementMethod.getInstance()
            }
        })


        dialogPurchaseNow.setOnClickListener {
            model?.let { it1 -> listener.onListener(it1) }
            dismiss()
        }
        dialogCancel.setOnClickListener {
            dismiss()
        }


        setOutSideDismiss(true)
    }

    interface MemberUnitaryBuyListener{
        fun onListener(bean: BuyMemberPageEntity.ProductExt)
    }

}